﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Specialized;

namespace DevFx.Model
{
    /// <summary>
    /// 所有业务对象基类
    /// </summary>
    [Serializable]
    public class BaseModel
    {
        public BaseModel()
        {

        }

        public BaseModel(IDataReader reader)
        {
            this.IsNewModel = false;
        }

        private bool _IsNewModel = true;
        private bool _IsHavChanged = false;
        private string _serialNumber = string.Empty;

        /// <summary>
        /// 是否新创建的业务对象
        /// </summary>
        public bool IsNewModel
        {
            get { return _IsNewModel; }
            set { _IsNewModel = value; }
        }
        /// <summary>
        /// 是否有修改记录
        /// </summary>
        public bool IsHavChanged
        {
            get { return _IsHavChanged; }
            set { _IsHavChanged = value; }
        }
        /// <summary>
        /// 流程的SN号
        /// </summary>
        public string SerialNumber
        {
            get { return _serialNumber; }
            set { _serialNumber = value; }
        }
        private StringDictionary _BrokenRules = new StringDictionary();

        protected virtual void AddRule(string propertyName, string errorMessage, bool isBroken)
        {
            if (isBroken)
            {
                _BrokenRules[propertyName] = errorMessage;
            }
            else
            {
                if (_BrokenRules.ContainsKey(propertyName))
                {
                    _BrokenRules.Remove(propertyName);
                }
            }
        }

        protected virtual void ValidationRules()
        { }

        public bool IsValid
        {
            get
            {
                ValidationRules();
                return this._BrokenRules.Count == 0;
            }
        }

        public virtual string ValidationMessage
        {
            get
            {
                if (!IsValid)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (string messages in this._BrokenRules.Values)
                    {
                        sb.AppendLine(messages + "@");
                    }

                    return sb.ToString();
                }

                return string.Empty;
            }
        }


    }
}
